package com.stylefeng.guns.modular.utils;


import com.stylefeng.guns.common.persistence.model.ProjectConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnect {
	public static Connection getConnection(ProjectConfig config) throws ClassNotFoundException, SQLException
			 {
		
		Class.forName(config.getDbType());
		return DriverManager.getConnection(config.getDbUrl(),
				config.getDbUsername(), config.getDbPassword());
	}

	/**
	 * 测试连接,返回错误信息,无返回内容说明连接成功
	 * 
	 * @param config
	 * @return 返回错误信息,无返回内容说明连接成功
	 */
	public static boolean testConnection(ProjectConfig config) {
		Connection con = null;
		try {
			con = DBConnect.getConnection(config);
			// 不为空说明连接成功
			if (con == null) {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();

			return false;
		} finally {
			if (con != null) {
				try {
					con.close(); // 关闭连接,该连接无实际用处
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return true;
	}
}
